package com.yonyou.pmclouds.plan.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.billtype.util.BillTypeInfoQueryUtils;
import com.yonyou.pmclouds.billtype.util.DepartmentInfoQueryUtils;
import com.yonyou.pmclouds.businessobject.util.SortListUtil;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.plan.entity.ScheduleVO;
import com.yonyou.pmclouds.plan.rmiitf.IPlanVersionWebService;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleItemQueryService;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleQueryService;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description: 进度版本WEB端
 * @Author: huxin
 * @Date: 2020-03-17
 */

//@RestController
//@RequestMapping("/planAdjust")
@Deprecated
public class PlanVersionWebController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IScheduleQueryService scheduleQueryService;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IScheduleItemQueryService scheduleItemQueryService;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IScheduleItemQueryService itemQueryService;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IPlanVersionWebService iPlanVersionWebService;

    @Autowired
    private UserInfoQuery userInfoQuery;

    @Autowired
    private BillTypeInfoQueryUtils billTypeInfoQueryUtils;

    @Autowired
    private DepartmentInfoQueryUtils departmentInfoQueryUtils;

    /**
     * 对选定版本号和上个版本号的业务数据进行内容对比
     * @param pkSchedule  当前版本号主键
     * @param version  当前版本号
     * @return
     * @throws BusinessException
     */
    @GetMapping("/compare")
    public ScheduleVO compare(@RequestParam("pkSchedule") String pkSchedule,@RequestParam("version") Integer version) throws BusinessException {
        String preScheduleId = scheduleQueryService.findByPreId(pkSchedule, version);

        if(preScheduleId.equals(StringUtils.EMPTY)){
            throw new BusinessException("没有找到上个版本的信息");
        }

        List<ScheduleVO> scheduleVOS = scheduleQueryService.queryByIds(new String[]{pkSchedule, preScheduleId});

        setRelatedValue(scheduleVOS.toArray(new ScheduleVO[]{}));
        
        return iPlanVersionWebService.compare(scheduleVOS,pkSchedule,preScheduleId);
    }

    /**
     * 点击项目通过项目id联动计划
     * @param projectId
     * @return
     * @throws BusinessException
     */
    @GetMapping("/project")
    public List<ScheduleVO> queryProject(@RequestParam("projectId") String projectId) throws BusinessException {
        return scheduleQueryService.queryByProjectId(projectId);
    }

    /**
     * 翻译关联项的值
     *
     * @param scheduleVOS 计划
     */
    private void setRelatedValue(ScheduleVO[] scheduleVOS) throws BusinessException {
        Set<String> billTypeCodeSet = new HashSet<>();
        Set<String> pkDeptSet = new HashSet<>();
        List<ScheduleItemVO> itemVOS = new ArrayList<>();
        for (ScheduleVO scheduleVO : scheduleVOS) {
            for (ScheduleItemVO itemVO : scheduleVO.getScheduleItemVOS()) {
                getRelatedItem(itemVO, billTypeCodeSet, pkDeptSet, itemVOS);
            }
        }

        Map<String, String> billTypeNameMap = billTypeInfoQueryUtils.queryNameByType(
                billTypeCodeSet.toArray(new String[]{}));
        Map<String, String> departmentNameMap = departmentInfoQueryUtils.queryNameByPks(
                pkDeptSet.toArray(new String[]{}));

        for (ScheduleVO scheduleVO : scheduleVOS) {
            for (ScheduleItemVO itemVO : scheduleVO.getScheduleItemVOS()) {
                setRelatedItemValue(itemVO, billTypeNameMap, departmentNameMap);
            }
        }

        String[] itemUserFields = new String[]{ScheduleItemVO.MANAGER, ScheduleItemVO.CREATOR,
                ScheduleItemVO.PLAN_FINISHER};
        String[] itemUserNameFields = new String[]{ScheduleItemVO.MANAGER_NAME,
                ScheduleItemVO.CREATOR_NAME,
                ScheduleItemVO.PLAN_FINISHER_NAME};
        userInfoQuery.tranUserInfoField(itemVOS.toArray(new ScheduleItemVO[]{}), itemUserFields, itemUserNameFields);

        String[] scheduleUserFields = new String[]{ScheduleVO.CREATOR};
        String[] scheduleUserNameFields = new String[]{ScheduleVO.CREATOR_NAME};
        userInfoQuery.tranUserInfoField(scheduleVOS, scheduleUserFields, scheduleUserNameFields);
    }

    /**
     * 获取需要翻译的关联项
     *
     * @param itemVO      任务节点
     * @param billTypeSet 任务关联的单据类型
     * @param pkDeptSet   任务关联的责任部门
     * @param itemVOS     所有的子级，翻译人员名称使用
     */
    private void getRelatedItem(ScheduleItemVO itemVO, Set<String> billTypeSet, Set<String> pkDeptSet, List<ScheduleItemVO> itemVOS) {
        itemVOS.add(itemVO);

        if (StringUtils.isNotEmpty(itemVO.getRelatedBillType())) {
            billTypeSet.add(itemVO.getRelatedBillType());
        }

        if (StringUtils.isNotEmpty(itemVO.getPkDepartment())) {
            pkDeptSet.add(itemVO.getPkDepartment());
        }

        if (itemVO.getChildren() != null && itemVO.getChildren().length > 0) {
            for (ScheduleItemVO childItemVO : itemVO.getChildren()) {
                getRelatedItem(childItemVO, billTypeSet, pkDeptSet, itemVOS);
            }
        }
    }

    /**
     * 对关联项进行翻译赋值
     *
     * @param itemVO            任务项
     * @param billTypeNameMap   关联单据类型描述汇总
     * @param departmentNameMap 关联部门描述汇总
     */
    private void setRelatedItemValue(ScheduleItemVO itemVO, Map<String, String> billTypeNameMap, Map<String, String> departmentNameMap) {
        if (StringUtils.isNotEmpty(itemVO.getRelatedBillType())) {
            itemVO.setRelatedBillTypeName(billTypeNameMap.get(itemVO.getRelatedBillType()));
        }
        if (StringUtils.isNotEmpty(itemVO.getPkDepartment())) {
            itemVO.setDepartmentName(departmentNameMap.get(itemVO.getPkDepartment()));
        }

        if (itemVO.getChildren() != null && itemVO.getChildren().length > 0) {
            for (ScheduleItemVO childItemVO : itemVO.getChildren()) {
                setRelatedItemValue(childItemVO, billTypeNameMap, departmentNameMap);
            }
        }
    }
}
